//
//  UIBarButtonItem+Category.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "UIBarButtonItem+Category.h"

@implementation UIBarButtonItem (Category)


+ (UIBarButtonItem *)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    CGSize maxSize = CGSizeMake(MAXFLOAT, 20);
    CGSize btnSize = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : btn.titleLabel.font} context:nil].size;
    
    btn.size = btnSize;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action
{
    //    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    //    [btn setImage:[UIImage imageNamed:highImageName] forState:UIControlStateNormal];
    //    btn.size = btn.currentImage.size;
    //    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //
    //    return [[UIBarButtonItem alloc] initWithCustomView:btn];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highImageName] forState:UIControlStateNormal];
    btn.size = CGSizeMake(44, 44);
    btn.imageEdgeInsets= UIEdgeInsetsMake(0, -(44 - btn.currentImage.size.width) * 0.5, 0, (44 - btn.currentImage.size.width) * 0.5);
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}



@end
